package com.zzy.util;

import java.awt.Insets;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.security.InvalidParameterException;

import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;



/**
 * 本地文件转pdf
 * @author guokaige
 *
 */
public class UrlToPdfUtil {
	protected static int topValue = 10;
	protected static int leftValue = 20;
	protected static int rightValue = 10;
	protected static int bottomValue = 10;
	protected static int userSpaceWidth = 1300;


	public static void doConversion2( String htmlDocument, String outputPath ,String fontPath) 
				throws InvalidParameterException, MalformedURLException, IOException {

		PD4ML pd4ml = new PD4ML();
			
		pd4ml.setHtmlWidth(userSpaceWidth); // set frame width of "virtual web browser" 
			
		// choose target paper format
		pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4)); 
			
		// define PDF page margins
		pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue)); 

		// source HTML document also may have margins, could be suppressed this way 
		// (PD4ML *Pro* feature):
		pd4ml.addStyle("BODY {margin: 0}", true);
			
		// If built-in basic PDF fonts are not sufficient or 
		// if you need to output non-Latin texts, TTF embedding feature should help 
		// (PD4ML *Pro*)
		//String srcPath=this.getClass().getClassLoader().getResource("").getPath(); 
		//pd4ml.useTTF(srcPath+"fonts", true);
		pd4ml.useTTF(fontPath, true);
		

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		// actual document conversion from HTML string to byte array
		pd4ml.render(new StringReader(htmlDocument), baos); 
		// if the HTML has relative references to images etc, 
		// use render() method with baseDirectory parameter instead
		baos.close();
		
		System.out.println( "resulting PDF size: " + baos.size() + " bytes" );
		// in Web scenarios it is a good idea to send the size with 
		// "Content-length" HTTP header

		File output = new File(outputPath);
		//创建父级目录
		if(!output.getParentFile().exists()){
			output.getParentFile().mkdirs();
		}
		java.io.FileOutputStream fos = new java.io.FileOutputStream(output);
		fos.write( baos.toByteArray() );
		fos.close();
		
		System.out.println( outputPath + "\ndone." );
	}
	
	
	/**
	 * 转码
	 * @Title:        title
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param path
	 * @param encoding
	 * @return
	 * @throws IOException    
	 * @version       V2.0 
	 * @author       guokaige
	 * @Date          2016-12-7 下午2:54:30
	 */
	public final static String readFile(String path, String encoding ) throws IOException {

		File f = new File( path );
		FileInputStream is = new FileInputStream(f);
		BufferedInputStream bis = new BufferedInputStream(is);
		
		ByteArrayOutputStream fos = new ByteArrayOutputStream();
		byte buffer[] = new byte[2048];

		int read;
		do {
			read = is.read(buffer, 0, buffer.length);
			if (read > 0) { 
				fos.write(buffer, 0, read); 
			}
		} while (read > -1);

		fos.close();
		bis.close();
		is.close();

		return fos.toString(encoding);
	}
	

}